<?php
/**
 * Created by PhpStorm
 * User: rain
 * Date: 2018/12/26
 * Time: 下午5:23
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * Class Orders
 * @property integer $id
 * @property string $order_code
 * @property integer $user_id
 * @property integer $goods_type
 * @property integer $goods_id
 * @property string $title
 * @property string $desc
 * @property integer $price
 * @property integer $status
 * @property integer $pay_type
 * @property string $prepay_id
 * @property string $logs
 * @property string $thirty_order
 * @property integer $pay_time
 * @property integer $refund_price
 * @property string $out_refund_no
 * @property integer $book_id
 * @property string $refund_logs
 * @property string $created_at
 * @property string $updated_at
 * @property string $date
 * @package App\Models
 */
class Orders extends Model
{
    protected $table = 'orders';

    const PAY_TYPE_MINI_WX = 1;
    public static $payTypeLabels = [
        self::PAY_TYPE_MINI_WX => '小程序微信支付',
    ];

    const STATUS_INIT    = 0;
    const STATUS_CLOSE   = 10;
    const STATUS_SUCCESS = 15;
    const STATUS_REFUND  = 20;
    public static $statusLabels = [
        self::STATUS_INIT => '待支付',
        self::STATUS_CLOSE => '关闭',
        self::STATUS_SUCCESS => '支付成功',
        self::STATUS_REFUND => '退款',
    ];


    const GOODS_TYPE_BOOK         = 0;
    const GOODS_TYPE_BOOK_CHAPTER = 1;
    const GOODS_TYPE_MEMBER       = 2;
    public static $typeLabels = [
        self::GOODS_TYPE_BOOK => '书籍',
        self::GOODS_TYPE_BOOK_CHAPTER => '书籍章节',
        self::GOODS_TYPE_MEMBER => '大会员',
    ];

    public function user()
    {
        return $this->belongsTo(Users::class);
    }

    public static function getOrderCode($prefix = '')
    {
        return $prefix . date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    }

}
